const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const ImportLog = sequelize.define('ImportLog', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  filename: {
    type: DataTypes.STRING(255),
    allowNull: false
  },
  total_records: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'total_records'
  },
  success_records: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'success_records'
  },
  failed_records: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'failed_records'
  },
  error_details: {
    type: DataTypes.TEXT('long'),
    allowNull: true,
    field: 'error_details',
    get() {
      const rawValue = this.getDataValue('error_details');
      return rawValue ? JSON.parse(rawValue) : null;
    },
    set(value) {
      this.setDataValue('error_details', value ? JSON.stringify(value) : null);
    }
  },
  status: {
    type: DataTypes.ENUM('success', 'partial', 'failed'),
    allowNull: false
  }
}, {
  tableName: 'import_logs',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: false,
  underscored: true
});

module.exports = ImportLog;
